package cn.wit.summit.chwan.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.interfaces.AdapterInterface;
import cn.wit.summit.chwan.models.db.HistorySearchTable;

/**
 * Created by tangbl on 2017/11/21.
 */

public class SearchAdapter extends BaseAdapter {

    private List<HistorySearchTable> mList;
    private Context context;
    private AdapterInterface mAdapterInterface;

    public SearchAdapter(Context context, List<HistorySearchTable> mList
            , AdapterInterface mAdapterInterface) {
        this.context = context;
        this.mAdapterInterface = mAdapterInterface;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        if (mList.size()>=5)
            return 5;
        else
            return mList.size();
    }

    @Override
    public HistorySearchTable getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_search_view, null);
            //初始化
            viewHolder.content_tv = (TextView) convertView.findViewById(R.id.keyWordTv);
            viewHolder.itemDeleteIv = (ImageView) convertView.findViewById(R.id.itemDeleteIv);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.content_tv.setText(mList.get(position).getKeyWord());
        viewHolder.itemDeleteIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapterInterface.onAdapterItemClick(v, position);
            }
        });

        return convertView;
    }

    public void deleteItem(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    class ViewHolder {
        public TextView content_tv;
        public ImageView itemDeleteIv;
    }
}
